ihelpuoo.namespace("ihelpuoo.widget.tree");

ihelpuoo.widget.tree.TreeNodeView = function(gasket
											,ownView
											,ptnName)
{
	this.ownView = ownView;
	this.ptnName = (ptnName?ptnName:"eve.pattern.tree.Node");
	this.view = new ihelpuoo.widget.View(gasket,this.ptnName);
	this.parent = null;
	this.children = new Array();
}

ihelpuoo.widget.tree.TreeNodeView.prototype
=
{
	getId: function()
	{
		return this.obj.id;
	}

	,
	clear: function()
	{
		this.degisterAllChildren();
		this.children = new Array();
		if(this.oSub)
			ihelpuoo.dom.clearAllChildren(this.oSub);	
	}
	
	,
	degisterMe: function()
	{
		this.ownView.degisterNode(this.getId());
		this.degisterAllChildren();
	}
	
	,
	degisterAllChildren: function()
	{
		if(this.children)
			for(var i=0;i<this.children.length;i++)
			{
				this.children[i].degisterMe();
			}
	}
	,
	draw : function(obj)
	{
		if(obj)
		{
			if(this.obj && this.obj.id != obj.id)
			{
				this.ownView.degisterNode(this);
				this.obj = obj;
				this.ownView.registerNode(this);
			}
			else
				this.obj = obj;
		}
		if(this.view.isNeverBeDrawn())
		{
			this.view.preparePattern();
			this.oSub = this.view.getElement("sub");
			this.oArena = this.view.getElement("arena");
			if(this.obj)
				this.ownView.registerNode(this);
			
			this.oName = this.view.getElement("name");
			ihelpuoo.widget.addViewEvent(this.ownView,this.oName,"click",this.onClick);
			this.oName.setAttribute("nodeId",this.getId());
			
			this.oHandle = this.view.getElement("handle");
			ihelpuoo.widget.addViewEvent(this.ownView,this.oHandle,"click",this.onSwitch);
			this.oHandle.setAttribute("nodeId",this.getId());
			
			this.oIcon = this.view.getElement("icon");
			
		}
		this.display();
		if(obj)
		{
			this.obj = obj;
			this.view.draw(obj);
			if(this.ownView.renderTreeNode)
				this.ownView.renderTreeNode(this);
			this.drawSub(obj.children);
		}
	}
	
	,
	drawSub : function(list)
	{
		this.clear();
		this.children = new Array();
		if(list && list.length && list.length>0)
		{
			for(var i=0;i<list.length;i++)
			{
				var nd = list[i];
				var subView = new ihelpuoo.widget.tree.
									TreeNodeView(this.oArena
												,this.ownView
												,this.ptnName);
				subView.draw(nd);
				subView.parent = this;
				this.children.push(subView);
			}
			this.open();
		}
		else
		{
			this.close();
		}
	}
	
	,
	onClick: function()
	{
		ihelpuoo.event.cancelBubble(ihelpuoo.Event(arguments));
		var ele = ihelpuoo.event.srcElement(this);
		var nodeId = parseInt(ele.getAttribute("nodeId"));
		var tree = ihelpuoo.widget.getViewByElement(ele);
		var node = tree.getNode(nodeId);
		node.open();
		node.ownView.setActivedNode(node);
	}
	
	,
	actived: function()
	{
		this.oName.className = this.oName.getAttribute("class_actived");
	}
	
	,
	disActived: function()
	{
		this.oName.className = this.oName.getAttribute("class_normal");
	}
	
	,
	open: function()
	{
		if((!this.children || this.children.length==0) && this.ownView.parent.draw)
			this.ownView.parent.draw(this.getId());
			
		if(this.children && this.children.length>0)
		{
			ihelpuoo.dom.showElement(this.oSub);
			this.oIcon.src = this.oIcon.getAttribute("src_opened");
			this.oHandle.src = this.oHandle.getAttribute("src_opened");
		}
	}
	
	,
	onSwitch: function()
	{
		var ele = ihelpuoo.event.srcElement(this);
		var nodeId = parseInt(ele.getAttribute("nodeId"));
		var tree = ihelpuoo.widget.getViewByElement(ele);
		var node = tree.getNode(nodeId);
		node.showHide();
	}
	
	,
	showHide: function()
	{
		if(this.oSub.style.display=="none")
			this.open();
		else
			this.close();
	}
	
	,
	close: function()
	{
		if(this.oSub)
		{
			this.oSub.style.display = "none";
			this.oIcon.src = this.oIcon.getAttribute("src_closed");
			this.oHandle.src = this.oHandle.getAttribute("src_closed");
		}
	}

	// <WEBOO:INHERIT extends="ihelpuoo.widget.View">
	,display: function(){this.view.display();}
	,getElement: function(key){return this.view.getElement(key);}
	,isNeverBeDrawn: function(){return this.view.isNeverBeDrawn();}
	,preparePattern: function(){this.view.preparePattern();}
	,hide: function(){this.view.hide();}
	,set: function(key,value){this.view.set(key,value);}
	,getString: function(key,defValue){return this.view.getString(key,defValue);}
	,getInt: function(key,defValue){return this.view.getInt(key,defValue);}
	,getBoolean: function(key,defValue){return this.view.getBoolean(key,defValue);}
	,addEvent: function(key,eventName,fnHandler,bCapture){this.view.addEvent(key,eventName,fnHandler,bCapture);}
	,removeEvent: function(key,eventName,fnHandler,bCapture){this.view.removeEvent(key,eventName,fnHandler,bCapture);}
	// </WEBOO:INHERIT>
}